package com.jtbd.encoder;

import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

@Component
public class CustomBCryptPasswordEncoder extends BCryptPasswordEncoder {
    public boolean matches(CharSequence rawPassword, String encodedPassword) {
        if (ObjectUtils.isEmpty(rawPassword) || ObjectUtils.isEmpty(encodedPassword)) {
            return false;
        }
        String rawPasswordString = rawPassword.toString();
        if (BCrypt.checkpw(rawPasswordString, encodedPassword)) {
            return true;
        }
        throw new InternalAuthenticationServiceException("密码验证失败");
    }
}
